<?php
//动态更新
function echoValue($value)
{
	return $value;
}

function getUserName()
{
	global $_FANWE;
	return $_FANWE['user']['user_name'];
}

function getAuctionMenu()
{
	global $_FANWE;
	if($_FANWE['group_id'] == 2)
	{
		$current = '';
		if(MODULE_NAME == 'auction')
			$current = 'current';
		
		return '<li class="i small '.$current.'"><a class="small" href="'.U('auction/index').'">推广竞拍</a></li>';
		
	}
	
	return '';
}

function echoScript()
{
	global $_FANWE;
	$script = '';
	$script .= $_FANWE['ucdata'];
	if(file_exists(FANWE_ROOT.'./public/cron.lock'))
		$script .= tplFetch('inc/ajax/corn');
	return $script;
}

function getUserInfo()
{
	return tplFetch('inc/user_info');
}

function getDisplayMode()
{
	global $_FANWE;
	static $is_check = NULL;

	if($is_check === NULL)
	{
		$display_modes = array('grid','row');
		if(empty($_FANWE['cookie']['display_mode']) || !in_array($_FANWE['cookie']['display_mode'],$display_modes))
		{
			$_FANWE['cookie']['display_mode'] = 'grid';
		}

		$is_check = true;
	}

	return $_FANWE['cookie']['display_mode'];
}

function getGoodsDynamics($ids)
{
	global $_FANWE;

	$result= '';

	$rand = abs(intval($_FANWE['setting']['goods_click_add_rand']));
	$add_num = abs(floatval($_FANWE['setting']['goods_click_add_num']));

	if(!empty($ids))
	{
		$result = "var goodsDynamics = {";

		$res = DB::query('SELECT id,bought,post_count,collect_count,click_count,add_time
					FROM '.DB::table('goods').' WHERE id in ('.$ids.')');
		$first = '';
		while($data = DB::fetch($res))
		{
			if($add_num > 0)
				$data['click_count'] += ceil($add_num * (gmtTime() - $data['add_time']) / 3600);

			if($rand > 0)
				$data['click_count'] += mt_rand(0,$rand);

			//$data['post_count'] = S('Bbs')->getPostCountByGoods($data['id']);
			$result .= $first.'"g_'.$data['id'].'":{"bought":'.$data['bought'].',"post":'.$data['post_count'].',"collect":'.$data['collect_count'].',"click":'.$data['click_count'].'}';
			$first = ',';
		}

		$result .= "};";
	}

	return $result;
}

function getSiteDynamic($id)
{
	$result= '';
	$id = intval($id);

	if($id > 0)
	{
		$site = DB::fetchFirst('SELECT click_count,best_count FROM '.DB::table('site').' WHERE id ='.$id.' AND status = 1');
		if($site)
		{
			$site['custom_count'] = DB::resultFirst('SELECT COUNT(user_id) FROM '.DB::table('user_site').' WHERE site_id = '.$id);

			$result = 'var siteDynamic = {"click_count":'.$site['click_count'].',"best_count":'.$site['best_count'].',"custom_count":'.$site['custom_count'].'};';
		}
	}

	return $result;
}

function getUserCustomSite()
{
	$args['custom_site_ids'] = explode(',',S('User')->getCustomSite());
	return tplFetch('inc/ajax/custom_site',&$args);
}

function getNewThreads($args)
{
	list($type,$num,$gid,$title) = explode(',',$args);
	$cache_file = FANWE_ROOT.'./public/dynamic/'.md5($type.'-'.$num.'-'.$gid.'-'.$title).'.php';
	if(checkFileCacheExpired($cache_file))
	{
		$arg['list'] = S('Bbs')->getNewThreads($type,$num,intval($gid));
		$arg['title'] = $title;

		if($gid > 0)
			$html = tplFetch('inc/common/goods_thread',&$arg);
		else
			$html = tplFetch('inc/common/right_thread',&$arg);

		writeFile($cache_file,$html);
	}
	else
		$html = file_get_contents($cache_file);

	return $html;
}

function getNewDiscussionPosts($num = 5)
{
	$cache_file = FANWE_ROOT.'./public/dynamic/'.md5('getNewDiscussionPosts-'.$num).'.php';
	if(checkFileCacheExpired($cache_file))
	{
		$args['list'] = S('Bbs')->getNewDiscussionPosts($num);
		$html = tplFetch('inc/common/right_thread_img',&$args);
        writeFile($cache_file,$html);
	}
	else
		$html = file_get_contents($cache_file);

	return $html;
}

function getGoodsBests($num = 5)
{
	$args['list'] = S('Goods')->getGoodsBests($num);
	return tplFetch('inc/common/right_goods_img',&$args);
}

function getGoodsClickRank($num = 5)
{
	$args['list'] = S('Goods')->getGoodsClickRank($num);
	return tplFetch('inc/common/right_rank',&$args);
}

function getSiteComment($num = 5)
{
	$args['list'] = S('Site')->getSiteComment($num);
	return tplFetch('inc/common/right_site',&$args);
}

function getCommentForm($show_star = 1)
{
	$args['show_star'] = $show_star;
	return tplFetch('inc/comment_form',&$args);
}

/*2011-02-28*/
function getGoodsComment($id)
{
	global $_FANWE;
	$id = intval($id);
	$page_args = array(
		'id' => $id,
	);

	$msg_count = DB::resultFirst('SELECT COUNT(*) FROM '.DB::table('message')."
			WHERE rec_module = 'Goods' AND rec_id = '$id'");

	$pager = buildPage('m/show',$page_args,$msg_count,$_FANWE['page'],10);

	$limit = (empty($pager['limit'])) ? '' : ' LIMIT '.$pager['limit'];

	$res = DB::query('SELECT user_name,content FROM '.DB::table('message')."
			WHERE rec_module = 'Goods' AND rec_id = '$id' ORDER BY id DESC $limit");
	while($data = DB::fetch($res))
	{
		$msg_list[] = $data;
	}

	$args['msg_list'] = $msg_list;
	$args['pager'] = $pager;

	return tplFetch('mobile/comment',$args);
}

/*2011-02-28*/
function getTopBestGroup()
{
	global $_FANWE;
	$list = S('Goods')->getGoodsNowList(" AND g.is_best = 1 AND gk.city = '".$_FANWE['current_city']['name']."'",'g.sort ASC,g.id DESC',',g.big_img',"0,1");

	if(count($list['list']) > 0)
	{
		$list = current($list['list']);
		$args['best_group'] = $list;
		return tplFetch('inc/common/best_group',$args);
	}
	else
		return '';
}

function getAuctionPage()
{
	global $_FANWE;
	$args['auction_hot_list'] = S('auction')->getHotList();
	$args['auction_list'] = S('auction')->getList();
	return tplFetch('inc/common/auction_list',$args);
}

function getAuctionInfo($id)
{
	global $_FANWE;
	$args['auction'] = S('auction')->getItem($id);
	return tplFetch('inc/common/auction_info',$args);
}

function advLayout($args)
{
	list($id,$count,$target) = explode(',',$args);
	unset($args);
	$ap = getAdvLayout($id,$count,$target);
	if(empty($ap))
		return '';

	$style = $ap['style'];
	unset($ap['style']);

	$cache_key = 'adv_position/'.$ap['id'];

	if($ap['is_flash'] == 1 && !empty($ap['flash_style']))
	{
		$args['adv_path'] = SITE_PATH."/public/adflash/".$ap['flash_style'].".swf";
		$args['adv_pics']="";
		$args['adv_texts']="";
		$args['adv_links']="";

		$jg = '';
		foreach($ap['adv_list'] as $adv)
		{
			$args['adv_pics'].=$jg.SITE_PATH.$adv['code'];
			$args['adv_texts'].=$jg.$adv['desc'];
			$args['adv_links'].=$jg.$adv['url'];
			$jg = "|";
		}

		unset($ap['adv_list']);

		$args['adv_position'] = $ap;
	}
	else
	{
		$args['adv_list'] = $ap['adv_list'];
	}

	unset($ap);

	return tplString($style,$cache_key,$args);
}

function checkFileCacheExpired($file,$time = 60)
{
	if(!filemtime($file) || time() - filemtime($file)  > $time)
		return true;
	else
		return false;
}
?>